package fr.orleans.publique;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import entities.Match;
import entities.Staff;
import entities.Team;
import entities.Tournament;
import fr.orleans.tools.JSFServices;
import fr.orleans.tools.ManagedBeans;

@ManagedBean
@ViewScoped
public class TournamentDetail extends ManagedBeans{

	private Tournament oneTournament;
	private List<Match> listMatch;
	private List<Team> listTeam;
	private Map<Team, Staff> mapTeamStaff;

	public TournamentDetail(){
		try{
			this.oneTournament = (Tournament) JSFServices.getParameterInRequestMap("tournament");
		}catch(Exception e){
			
		}
	}
	
	public Tournament getOneTournament() {
		if(this.oneTournament == null)
			this.oneTournament = (Tournament) JSFServices.getParameterInRequestMap("tournament");
		
		return oneTournament;
	}

	public void setOneTournament(Tournament oneTournament) {
		this.oneTournament = oneTournament;
	}

	public List<Match> getListMatch() {
		if(this.listMatch == null)
			this.listMatch = new ArrayList<>(this.oneTournament.getListMatchs());
			
		return listMatch;
	}

	public void setListMatch(List<Match> listMatch) {
		this.listMatch = listMatch;
	}

	public List<Team> getListTeam() {
		if(this.listTeam == null)
			this.listTeam = new ArrayList<>(this.oneTournament.getListTeams());
			
		return listTeam;
	}

	public void setListTeam(List<Team> listTeam) {
		this.listTeam = listTeam;
	}

	public Map<Team, Staff> getMapTeamStaff() {
		if(this.mapTeamStaff == null){
			this.mapTeamStaff = new HashMap<>();
			
			for(Team t : this.listTeam){
				if(t.getListStaffMembers().size() > 0){
					this.mapTeamStaff.put(t,t.getListStaffMembers().iterator().next());
				}
			}
		}
		return mapTeamStaff;
	}

	public void setMapTeamStaff(Map<Team, Staff> mapTeamStaff) {
		this.mapTeamStaff = mapTeamStaff;
	}
	
	public String goToDetailMatch(Match oneMatch){
		JSFServices.putParameterInRequestMap("match", oneMatch);
		return "/modules/public/detailMatch.jsf";
	}
	
	public String goToDetailTeam(Team oneTeam){
		JSFServices.putParameterInRequestMap("team", oneTeam);
		return "/modules/public/detailTeam.jsf";
	}
}